package com.smrs.asn.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;

import com.jof.framework.dao.hibernate.BaseDAOHibernateImpl;
import com.jof.framework.util.Pager;
import com.smrs.asn.model.AsnHeaderModel;
import com.smrs.asn.model.AsnHeaderSearchModel;
import com.smrs.util.DateCommonUtil;


public class AsnHeaderDao extends BaseDAOHibernateImpl<AsnHeaderModel, Long>{
	
	@SuppressWarnings("unchecked")
	public Pager<AsnHeaderModel> findAsnHeader(AsnHeaderSearchModel asnHeaderSearch, Pager<AsnHeaderModel> pager){
		String queryString = " from AsnHeaderModel where 1=1 ";
		ArrayList<Object> list = new ArrayList<Object>(); 
		if(asnHeaderSearch.getStatus()!=null){
			queryString = queryString+" and status=?";
			list.add(asnHeaderSearch.getStatus());
		}
		if(asnHeaderSearch.getStartDate() !=null){
			queryString= queryString + " and creationDate >=? ";
			Date mixOfDate = DateCommonUtil.getBeginOfDate(asnHeaderSearch.getStartDate());
			list.add(mixOfDate);			
		}
		if(asnHeaderSearch.getEndDate()!=null){
			queryString= queryString + " and creationDate <? ";
			Date endOfDate = DateCommonUtil.getEndOfDate(asnHeaderSearch.getEndDate());
			list.add(endOfDate);			
		}
		if(asnHeaderSearch.getStoreId()!=null){
			queryString= queryString + " and storeId =? ";
			list.add(asnHeaderSearch.getStoreId());	
		}
		if(StringUtils.isNotEmpty(asnHeaderSearch.getStoreCode())){
			queryString= queryString + " and storeCode like  '%"+asnHeaderSearch.getStoreCode()+"%'";
		}
		if(StringUtils.isNotEmpty(asnHeaderSearch.getStoreName())){
			queryString= queryString + " and storeName like  '%"+asnHeaderSearch.getStoreName()+"%'";
		}
		
		Query query = this.createHQLQuery(queryString, list.toArray());
		List resultList = query.list();
		long totalRecords = this.countHQLResult(queryString, list.toArray());
		pager = Pager.cloneFromPager(pager, totalRecords, resultList);
		return pager;
	}	
	
}
